﻿using DomainService.IService.Sys;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Model.BusinessModel.Input;
using Model.BusinessModel.OutPut;
using Model.DomainModel.Sys;
using OverallAuth_WebApi.PlugInU;
using System.IdentityModel.Tokens.Jwt;
using Utility;
using Utility.Enum;

namespace OverallAuth_WebApi.Controllers.Sys
{
    /// <summary>
    /// 用户模块
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    [ApiExplorerSettings(GroupName = nameof(ModeuleGroupEnum.SysUser))]
    public class SysUserController : BaseController
    {
        #region 构造实列化

        /// <summary>
        /// 用户服务
        /// </summary>
        public ISysUserService _userService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userService"></param>
        public SysUserController(ISysUserService userService)
        {
            _userService = userService;
        }

        #endregion

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous] // 不验证权限
        public ReceiveStatus<LoginOutPut> Login(LoginInput loginModel)
        {
            var result = _userService.GetUserMsg(loginModel.UserName ?? string.Empty, loginModel.Password ?? string.Empty);
            if (result.success)
            {
                var loginResult = result.data.First();
                var tokenResult = JwtPlugIn.BuildToken(loginModel);
                loginResult.Token = tokenResult.Token;
                loginResult.ExpiresDate = tokenResult.ExpiresDate;
                result.data = new List<LoginOutPut>() { loginResult };
            }
            return result;
        }

        /// <summary>
        /// 获取Token
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        [HttpGet]
        [AllowAnonymous]
        public string GetToken(string userName, string password)
        {
            var loginResult = JwtPlugIn.BuildToken(new LoginInput { UserName = userName, Password = password });

            return loginResult.Token ?? string.Empty;
        }

        /// <summary>
        /// 测试Autofac
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public string TestAutofac()
        {
            return _userService.TestAutofac();
        }

        /// <summary>
        /// 查询所有用户
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<SysUser> GetAllUser()
        {
            return _userService.GetAllUser();
        }

        /// <summary>
        /// 测试异常
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public string TestException()
        {
            throw new Exception("系统发生错误！");
        }

        ///// <summary>
        ///// 获取登录用户信息
        ///// </summary>
        //[HttpGet]
        //public LoginOutPut GetLoginUserMsg()
        //{

        //    return GetLoginUserMsg();

        //    //var toknes = Request.Headers["Authorization"].ToString().Replace("Bearer", "");

        //    //var loginResult = JwtPlugInUnit.ParseJwtAsync(token);

        //    //var ddd = loginResult;
        //}
    }
}
